<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class GoodsCarController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {   

        return view('home.goods.goodscar');
    }

    public function getAdd(){
        $gid = $_GET['gid'];//相应的商品ID
        $goodsnumber = $_GET['goodsnumber'];//商品数量直接存入购物车表
        $username = session('homeindex_username');//用户名可以获取到相应的用户ID
        $goodscarnumber = $_GET['goodscarnumber'];//获取购物车中的商品数量

        //根据用户名查询相应的用户ID
        $id = DB::table('tbl_home_user')->where('username',$username)->first();
        $uid = $id['id'];
        //查询这个商品的单价
        $sigleprice = DB::table('tbl_goods')->where('id',$gid)->first();
        $price = $sigleprice['goodsprice'];
        $goodscartotalprice = $goodsnumber*$price;
        //建立一个需要插入或更新数据库的空数组并将所需数据压入其中
        $insertcar = array();
        $insertcar['uid'] = $uid;
        $insertcar['gid'] = $gid;
        $insertcar['goodscartime'] = date('Y-m-d H:i:s',time());
        $insertcar['goodsnumber'] = $goodsnumber;
        $insertcar['goodscarnumber'] = $goodscarnumber;
        $insertcar['goodscartotalprice'] = $goodscartotalprice;
        //先查询数据库所有的uid和gid字段并判断用户和商品ID是否都已经一同存在数据库
        //（若都存在则只需要更新购物车的商品数量即可）
        $res =  DB::table('tbl_goodscar')->insert($insertcar);
        if($res){
          echo '成功加入购物车';
          session(['carsum'=>$goodscarnumber]);
        }

    }
    public function getContent(){

        //先根据登录的用户获取到用户的ID
       $u = DB::table('tbl_home_user')->where('username',session('homeindex_username'))->first();
       $uid = $u['id'];
       //关联查询登录的用户旗下的购物车信息
       $data = DB::table('tbl_goodscar')
       ->join('tbl_goods','tbl_goods.id','=','tbl_goodscar.gid')
       ->where('uid',$uid)->get();
       //由于关联查询的覆盖 单独取出购物车相对应的ID编号
       $carid = DB::table('tbl_goodscar')->where('uid',$uid)->lists('id');
       //将得到的信息遍历取其每个的总价压入一个数组
       $arr = [];
       foreach($data as $k=>$v){
            $arr[] = $v['goodscartotalprice'];
       }
       //将新组成的数组进行求和后得到的合计金额 即时存入数据库
       $totalprice = array_sum($arr);
        return view('home.goods.goodscar',['data'=>$data,'totalprice'=>$totalprice,'carid'=>$carid]);
    }

    public function getPaydata(){
        $goodsnumber = $_GET['goodsnumber'];//商品的数量
        $goodscartotalprice = $_GET['goodscartotalprice'];//商品小计
        $checkbox = isset($_GET['checkbox'])?$_GET['checkbox']:'undefined';
        $id = $_GET['id'];//购物车的ID
        //将商品个数和商品总价更新到数据库
        $res = DB::table('tbl_goodscar')->where('id',$id)->update([
                    'goodsnumber'=>$goodsnumber,
                    'goodscartotalprice'=>$goodscartotalprice
            ]);
        //查询对应着购物车ID下的商品GID
       $giddata = DB::table('tbl_goodscar')->where('id',$id)->first();
       $gid = $giddata['gid'];
       //根据查询到的gid来查询对应商品的单价
        $data = DB::table('tbl_goods')->where('id',$gid)->first();
        $price = $data['goodsprice'];
            if($checkbox == 'checked'){
                echo $price;
            }

        }
    
}
